<template>
	<view>
		<!-- 搜索 -->
		<view class="search-box">
			<my-search @myClick="gotoSearch"></my-search>
		</view>
		<!-- 轮播图区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, index) in swiperList" :key="item.goods_id">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id' + item.goods_id">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类导航区域 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, index) in navList" :key="index" @click="navHandleClick(item)">
				<image :src="item.image_src"></image>
			</view>
		</view>
		<!-- 楼层 -->
		<view class="floor-list">
			<!-- 每一层的item项 -->
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 楼层标题 -->
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				<!-- 楼层图片区域 -->
				<view class="box">
					<!-- 左边的大图 -->
					<navigator :url="item.product_list[0].url" class="left">
						<image :src="item.product_list[0].image_src" :style="{ width: item.product_list[0].image_width + 'rpx' }" mode="widthFix"></image>
					</navigator>
					<!-- 右边的图片 -->
					<view class="right">
						<navigator :url="item2.url" class="right-box" v-for="(item2, index2) in item.product_list" :key="index2">
							<image :src="item2.image_src" v-if="index2 !== 0" :style="{ width: item2.image_width + 'rpx' }" mode="widthFix"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { h } from 'vue'
export default {
	data() {
		return {
			swiperList: [],
			navList: [],
			floorList: []
		}
	},
	onLoad() {
		this.getSwiperList()
		this.getNavList()
		this.getFloorList()
	},
	methods: {
		async getSwiperList() {
			const { data: res } = await uni.$http.get('/api/public/v1/home/swiperdata')
			// 请求失败
			if (res.meta.status != 200) return uni.$showMsg()
			// 请求成功
			// 换地址成http
			res.message.forEach((item) => {
				item.image_src = item.image_src.replace('https', 'http')
			})
			this.swiperList = res.message
			uni.$showMsg('数据请求成功')
		},
		async getNavList() {
			// /api/public/v1/home/catitems
			const { data: res } = await uni.$http.get('/api/public/v1/home/catitems')
			// 请求失败
			if (res.meta.status != 200) return uni.$showMsg()
			// 请求成功
			// 换地址成http
			res.message.forEach((item) => {
				item.image_src = item.image_src.replace('https', 'http')
			})
			this.navList = res.message
			uni.$showMsg('数据请求成功')
		},
		async getFloorList() {
			// /api/public/v1/home/catitems
			const { data: res } = await uni.$http.get('/api/public/v1/home/floordata')
			// 请求失败
			if (res.meta.status != 200) return uni.$showMsg()
			// 请求成功
			res.message.forEach((floor) => {
				floor.floor_title.image_src = floor.floor_title.image_src.replace('https', 'http')
				floor.product_list.forEach((product) => {
					product.url = product.navigator_url.replace('/pages', '/subpkg/goods_list')
					product.image_src = product.image_src.replace('https', 'http')
				})
			})
			this.floorList = res.message
			uni.$showMsg('数据请求成功')
		},
		navHandleClick(item) {
			console.log(item.name)
			if (item.name === '分类') {
				uni.switchTab({
					url: '/pages/cate/cate'
				})
			}
		},
		gotoSearch() {
			uni.navigateTo({
				url: '/subpkg/search/search'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.search-box {
	z-index: 100;
	position: sticky;
	top: 0;
	width: 100%;
}

swiper {
	height: 330rpx;

	.swiper-item {
		width: 100%;
		height: 100%;

		image {
			width: 100%;
			height: 100%;
		}
	}
}

.nav-list {
	display: flex;
	justify-content: space-around;
	margin: 15px 0;

	.nav-item {
		image {
			width: 128rpx;
			height: 140rpx;
		}
	}
}
.floor-list {
	.floor-title {
		display: flex;
		width: 100%;
		height: 60rpx;
	}
	.box {
		display: flex;
		padding-left: 10rpx;
		.right {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-around;
		}
	}
}
</style>
